<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="isShow" @close="cancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptFrom" label-width="120px" :model="ruleFrom" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="ruleFrom.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="ruleFrom.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="ruleFrom.manager" style="width:80%" placeholder="请选择" @focus="setmanager" @blur="ruleManager">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="ruleFrom.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="isOK">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartmentData, addDepartment, getDepartmentById, eidtDeparmentById } from '@/api/department'
import { employeesSimple } from '@/api/employees'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    const deptName = async(ruler, value, callback) => {
      const { depts } = await getDepartmentData()
      let isRepeat = ''
      if (this.ruleFrom.id) {
        isRepeat = depts.filter(item => this.itemData.pid === item.pid).some(item => item.name === value && item.id !== this.itemData.id)
      } else {
        isRepeat = depts.filter(item => this.itemData.id === item.pid).some(item => item.name === value)
      }

      isRepeat ? callback(new Error('命名重复')) : callback()
    }
    const deptCode = async(ruler, value, callback) => {
      const { depts } = await getDepartmentData()
      const isRepeat = depts.some(item => item.code === value && item.id !== this.itemData.id)
      isRepeat ? callback(new Error('code编码重复')) : callback()
    }
    return {
      ruleFrom: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      options: '',
      rules: {
        code: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50位字符', trigger: 'blur' },
          { trigger: 'blur', validator: deptCode }
        ],
        name: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50位字符', trigger: 'blur' },
          { trigger: 'blur', validator: deptName }
        ],
        manager: [
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1-50位字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.ruleFrom.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async setmanager() {
      this.options = await employeesSimple()
    },
    // 负责人的失焦矫正
    ruleManager() {
      setTimeout(() => {
        this.$refs.deptFrom.validateField('manager')
      }, 200)
    },
    // 点击确定
    async isOK() {
      try {
        await this.$refs.deptFrom.validate()
        if (this.ruleFrom.id) {
          await eidtDeparmentById(this.ruleFrom)
        } else {
          await addDepartment({ ...this.ruleFrom, pid: this.itemData.id })
        }
        this.$emit('addDept')
        this.$message.success('操作成功')
        this.$emit('update:isShow', false)
      } catch (err) {
        console.log(err)
      }
    },
    cancel() {
      this.ruleFrom = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.deptFrom.resetFields()
      this.$emit('update:isShow', false)
    },
    // 点击编辑
    async editDepartment(id) {
      this.ruleFrom = await getDepartmentById(id)
    }
  }
}
</script>

<style>

</style>
